import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router'

import { useRouteStore } from '@/stores/modules/index';
import { routesList } from './routes'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      // 404页面配置
      path: '/:catchAll(.*)',
      component: () => import('@/views/notPage/index.vue'),
      meta: {
        transition: 'slide-right',
        menuName: '404'
      },
    },
    {
      redirect: '/',
      path: '/utools',
    }

  ]
})
console.log(router);
// // 防止首次或者刷新界面路由失效
let registerRouteFresh = true
router.beforeEach((to, from, next) => {

  // 首次进来加载一次
  if (registerRouteFresh) {

    let store = useRouteStore()
    // 判读pina中有没有数据
    if (store.routeList.length < 1) {
      store.setRouteList(routesList, router)
      next({ ...to, replace: true })
    } else {
      next()
    }
    registerRouteFresh = false
  } else {
    next()
  }
}

)


export default router
